package com.lo.handlers;

import com.lo.enums.EnumVip;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


//jdbc设置参数  javatype转换成jdbctype
public class EnumVipTypeHandler implements TypeHandler<EnumVip> {

    @Override
    public void setParameter(PreparedStatement ps, int i, EnumVip parameter, JdbcType jdbcType) throws SQLException {
        int enumTypeCode = parameter.getCode();
        ps.setObject(i,enumTypeCode);
    }

    @Override
    public EnumVip getResult(ResultSet rs, String columnName) throws SQLException {
        Integer typeCode = (Integer) rs.getObject(columnName);
        return EnumVip.getByCode(typeCode);
    }

    @Override
    public EnumVip getResult(ResultSet rs, int columnIndex) throws SQLException {
        Integer typeCode = (Integer) rs.getObject(columnIndex);
        return EnumVip.getByCode(typeCode);
    }

    @Override
    public EnumVip getResult(CallableStatement cs, int columnIndex) throws SQLException {
        Integer typeCode = (Integer) cs.getObject(columnIndex);
        return EnumVip.getByCode(typeCode);
    }


}
